package br.com.decsoft.lv.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import br.com.decsoft.lv.jdbc.ConnectionFactory;
import br.com.decsoft.lv.model.Grupo;

public class GrupoDAO {
	private Connection conn = null;
	private Statement stm = null;
	
	public GrupoDAO() throws SQLException {
		this.conn = ConnectionFactory.getConnection();
	}

	@SuppressWarnings("unchecked")
	public ArrayList listar() throws Exception {
		ArrayList<Grupo> lista = null;
		try {
			stm = conn.createStatement();
			
			//System.out.println("Sql:" + sql);
			
			ResultSet rs = stm.executeQuery("select GRUPICOD,GRUPA60DESCR from GRUPO order by GRUPA60DESCR");
			lista = new ArrayList<Grupo>();
			while (rs.next()) {
				Grupo grupo = new Grupo();
				
				if (rs.getString("GRUPICOD") != null)
					grupo.setCodigo(rs.getInt("GRUPICOD"));
				if (rs.getString("GRUPA60DESCR") != null)
					grupo.setDescricao(rs.getString("GRUPA60DESCR"));
				
				lista.add(grupo);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			stm.close();
			conn.close();
		}
		return lista;
	}

}
